﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq.Mapping;
using System.Data.Linq;
using CNdotnet.Ezsocio.Core.DataEntity;

namespace CNdotnet.Ezsocio.DataEntities
{
    [Table(Name = "dbo.Friendship")]
    public class Friendship : Entity
    {
        #region Private Fields

        private int _Id;

        private int _userId;

        private int _friendId;

        private DateTime _createDate;

        private bool _isConfirmed;

        private DateTime? _confirmDate;

        private int _groupId;

        private string _note;

        private EntityRef<FriendGroup> _friendGroup;

        private EntityRef<User> _user;

        private EntityRef<User> _friendUser;
        #endregion

        #region Constructor

        public Friendship()
        {
            this._friendGroup = default(EntityRef<FriendGroup>);
            this._user = default(EntityRef<User>);
            this._friendUser = default(EntityRef<User>);
        }

        #endregion

        #region Public Properties

        [Column(Name = "ID", AutoSync = AutoSync.OnInsert, IsDbGenerated = true, Storage = "_Id", DbType = "Int NOT NULL Identity", IsPrimaryKey = true)]
        public int ID
        {
            get
            {
                return this._Id;
            }
            set
            {
                if (this._Id != value)
                {
                    this.SendPropertyChanging("ID");
                    this._Id = value;
                    this.SendPropertyChanged("ID", value);
                }
            }
        }

        [Column(Name = "UserId", Storage = "_userId", DbType = "Int NOT NULL")]
        public int UserId
        {
            get
            {
                return this._userId;
            }
            set
            {
                if (this._userId != value)
                {
                    this.SendPropertyChanging("UserId");
                    this._userId = value;
                    this.SendPropertyChanged("UserId", value);
                }
            }
        }

        [Column(Name = "FriendId", Storage = "_friendId", DbType = "Int NOT NULL")]
        public int FriendId
        {
            get
            {
                return this._friendId;
            }
            set
            {
                if (this._friendId != value)
                {
                    if (this._user.HasLoadedOrAssignedValue)
                    {
                        throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
                    }
                    this.SendPropertyChanging("FriendId");
                    this._friendId = value;
                    this.SendPropertyChanged("FriendId",value);
                }
            }
        }

        [Column(Name = "CreateDate", Storage = "_createDate", DbType = "DateTime NOT NULL")]
        public DateTime CreateDate
        {
            get
            {
                return this._createDate;
            }
            set
            {
                if (this._createDate != value)
                {
                    this.SendPropertyChanging("CreateDate");
                    this._createDate = value;
                    this.SendPropertyChanged("CreateDate",value);
                }
            }
        }

        [Column(Name = "Confirmed", Storage = "_isConfirmed", DbType = "Bit NOT NULL")]
        public bool IsConfirmed
        {
            get
            {
                return this._isConfirmed;
            }
            set
            {
                if (this._isConfirmed != value)
                {
                    this.SendPropertyChanging("IsConfirmed");
                    this._isConfirmed = value;
                    this.SendPropertyChanged("IsConfirmed", value);
                }
            }
        }

        [Column(Name = "ConfirmDate", Storage = "_confirmDate", DbType = "DateTime")]
        public DateTime? ConfirmDate
        {
            get
            {
                return this._confirmDate;
            }
            set
            {
                if (this._confirmDate != value)
                {
                    this.SendPropertyChanging("ConfirmDate");
                    this._confirmDate = value;
                    this.SendPropertyChanged("ConfirmDate", value);
                }
            }
        }

        [Column(Name = "GroupId", Storage = "_groupId", DbType = "Int NOT NULL")]
        public int GroupId
        {
            get
            {
                return this._groupId;
            }
            set
            {
                if (this._groupId != value)
                {
                    if (this._friendGroup.HasLoadedOrAssignedValue)
                    {
                        throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
                    }

                    this.SendPropertyChanging("GroupId");
                    this._groupId = value;
                    this.SendPropertyChanged("GroupId", value);
                }
            }
        }

        [Column(Name = "Note", Storage = "_note", DbType = "VarChar(1024) NOT NULL", CanBeNull = false)]
        public string Note
        {
            get
            {
                return this._note;
            }
            set
            {
                if (this._note != value)
                {
                    this.SendPropertyChanging("Note");
                    this._note = value;
                    this.SendPropertyChanged("Note", value);
                }
            }
        }

        [Association(Name = "FriendGroup_Friendship", Storage = "_friendGroup", ThisKey = "GroupId", IsForeignKey = true)]
        public FriendGroup FriendGroup
        {
            get
            {
                return this._friendGroup.Entity;
            }
            set
            {
                FriendGroup previousValue = this._friendGroup.Entity;
                if (((previousValue != value)
                            || (this._friendGroup.HasLoadedOrAssignedValue == false)))
                {
                    if ((previousValue != null))
                    {
                        this._friendGroup.Entity = null;
                        previousValue.Friendships.Remove(this);
                    }
                    this._friendGroup.Entity = value;
                    if ((value != null))
                    {
                        value.Friendships.Add(this);
                        this._groupId = value.ID;
                    }
                    else
                    {
                        this._groupId = default(int);
                    }
                }
            }
        }

        [Association(Name = "User_Friendship", Storage = "_user", ThisKey = "UserId", IsForeignKey = true)]
        public User User
        {
            get
            {
                return this._user.Entity;
            }
            set
            {
                User previousValue = this._user.Entity;
                if (((previousValue != value)
                            || (this._user.HasLoadedOrAssignedValue == false)))
                {
                    if ((previousValue != null))
                    {
                        this._user.Entity = null;
                        previousValue.Friendships.Remove(this);
                    }
                    this._user.Entity = value;
                    if ((value != null))
                    {
                        value.Friendships.Add(this);
                        this._userId = value.ID;
                    }
                    else
                    {
                        this._userId = default(int);
                    }
                }
            }
        }

        [Association(Name = "User_Friendship", Storage = "_friendUser", ThisKey = "FriendId", IsForeignKey = true)]
        public User FriendUser {
            get {
                return this._friendUser.Entity;
            }
            set {
                User previousValue = this._friendUser.Entity;
                if (((previousValue != value)
                            || (this._friendUser.HasLoadedOrAssignedValue == false))) {
                    if ((previousValue != null)) {
                        this._friendUser.Entity = null;
                        previousValue.Friendships.Remove(this);
                    }
                    this._user.Entity = value;
                    if ((value != null)) {
                        value.Friendships.Add(this);
                        this._friendId = value.ID;
                    }
                    else {
                        this._friendId = default(int);
                    }
                }
            }
        }
        #endregion
    }
}
